package com.bdzl.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.bdzl.common.bean.BaseResponse;
import com.bdzl.daoRepository.AdminDao;
import com.bdzl.daoRepository.AgentDao;
import com.bdzl.daoRepository.CompanyDao;
import com.bdzl.daoRepository.DiseaseDao;
import com.bdzl.daoRepository.FeedbackDao;
import com.bdzl.exception.ParameterException;
import com.bdzl.pojo.Admin;
import com.bdzl.pojo.Agent;
import com.bdzl.pojo.Company;
import com.bdzl.pojo.Disease;
import com.bdzl.pojo.Feedback;
import com.bdzl.request.AddAdmin;
import com.bdzl.request.AddCompany;
import com.bdzl.request.AddDisList;
import com.bdzl.request.DelDislist;
import com.bdzl.request.Login;
import com.bdzl.request.ModisList;
import com.bdzl.request.UpdAdmin;
import com.bdzl.request.UpdDis;
import com.bdzl.response.AllDisss;
import com.bdzl.response.AllFb;
import com.bdzl.response.AllFbs;
import com.bdzl.response.Alladmins;
import com.bdzl.response.Allcompany;
import com.bdzl.response.Alldise;
import com.bdzl.response.LoginInfo;

@Service
public class AdminService {

	@Autowired
	AdminDao adminDao;

	@Autowired
	FeedbackDao feedbackDao;

	@Autowired
	AgentDao agentDao;

	@Autowired
	CompanyDao companyDao;

	@Autowired
	DiseaseDao diseaseDao;

	/**
	 * 后台登陆
	 * 
	 * @return
	 * @throws Exception
	 */
	@Transactional(readOnly = true)
	public BaseResponse login(Login login) throws Exception {
		LoginInfo data = new LoginInfo();

		Admin admin = adminDao.findByAccountAndPassword(login.getName(), login.getPassword());
		if (admin == null) {
			throw new ParameterException(301, "账号或者密码错误");
		} else {
			if (admin.getType() == 0) {

				throw new ParameterException(301, "账号已经被停用了，请联系超级管理员");
			} else {
				data.setAuthrity(admin.getAuthrity());
				data.setName(admin.getName());
				data.setAdminId(admin.getId());
			}
		}

		BaseResponse br = new BaseResponse();
		br.setData(data);
		br.setStateCode(101);
		br.setStateMessage("登陆成功");

		return br;
	}

	/**
	 * 获取所有管理员列表
	 * 
	 * @return
	 * @throws Exception
	 */
	@Transactional(readOnly = true)
	public BaseResponse allAdmin() throws Exception {

		List<Alladmins> data = new ArrayList<Alladmins>();

		List<Admin> list = adminDao.findByType(1);
		for (Admin admin : list) {
			Alladmins a = new Alladmins();
			a.setAdminId(admin.getId());
			a.setName(admin.getName());
			a.setAccount(admin.getAccount());
			a.setAuthrity(admin.getAuthrity());
			data.add(a);
		}
		BaseResponse br = new BaseResponse();
		br.setData(data);
		br.setStateCode(101);
		br.setStateMessage("获取列表成功");

		return br;
	}

	/**
	 * 添加管理员
	 * 
	 * @param addAdmin
	 * @return
	 * @throws Exception
	 */
	@Transactional(rollbackFor = Exception.class)
	public BaseResponse addadmin(AddAdmin addAdmin) throws Exception {
		Admin a = new Admin();

		a.setAccount(addAdmin.getAccount());
		a.setAuthrity(addAdmin.getAuthrity());
		a.setName(addAdmin.getName());
		a.setPassword(addAdmin.getPassword());
		a.setType(1);

		adminDao.save(a);

		BaseResponse br = new BaseResponse();
		br.setStateCode(100);
		br.setStateMessage("添加管理员成功");

		return br;

	}

	/**
	 * 删除管理员
	 * 
	 * @param adminId
	 * @return
	 * @throws Exception
	 */
	@Transactional(rollbackFor = Exception.class)
	public BaseResponse deladmin(Integer adminId) throws Exception {

		// 管理员停用

		adminDao.updatetype(adminId);

		BaseResponse br = new BaseResponse();
		br.setStateCode(103);
		br.setStateMessage("删除成功");

		return br;

	}

	/**
	 * 修改管理员信息
	 * 
	 * @param adminId
	 * @param updAdmin
	 * @return
	 * @throws Exception
	 */
	@Transactional(rollbackFor = Exception.class)
	public BaseResponse updadmin(Integer adminId, UpdAdmin updAdmin) throws Exception {
		Admin admin = adminDao.findById(adminId);
		if (admin == null) {
			throw new ParameterException(301, "修改的管理员不存在");
		}

		if (updAdmin.getPassword().equals("")  ) {
			adminDao.updateaps(adminId, updAdmin.getAccount(), updAdmin.getAuthrity(), updAdmin.getName());
		} else {
			adminDao.updateall(adminId, updAdmin.getAccount(), updAdmin.getAuthrity(), updAdmin.getPassword(),
					updAdmin.getName());
		}

		BaseResponse br = new BaseResponse();
		br.setStateCode(102);
		br.setStateMessage("修改成功");

		return br;
	}

	/**
	 * 获取意见列表
	 * 
	 * @return
	 * @throws Exception
	 */
	@Transactional(readOnly = true)
	public BaseResponse allfb(Integer pageIndex, Integer pageSize) throws Exception {
		AllFbs data = new AllFbs();

		// 总条数
		Integer totalSize = feedbackDao.findcount();

		List<AllFb> feedbacks = new ArrayList<AllFb>();

		List<Feedback> list = feedbackDao.findByState(1, new PageRequest(pageIndex, pageSize,
				new Sort(Direction.ASC, "type").and(new Sort(Direction.DESC, "time"))));

		for (Feedback feedback : list) {
			AllFb a = new AllFb();
			a.setFeedbackId(feedback.getId());
			a.setTime(feedback.getTime());
			a.setContent(feedback.getContent());
			a.setName(feedback.getName());
			a.setPhone(feedback.getTel());
			a.setType(feedback.getType());
			if (feedback.getAgentId() != null) {
				Agent agent = agentDao.findById(feedback.getAgentId());
				a.setFkname(agent.getName());
			}
			a.setFkname("游客");

			feedbacks.add(a);
		}

		data.setFeedbacks(feedbacks);
		data.setTotalSize(totalSize);

		BaseResponse br = new BaseResponse();
		br.setData(data);
		br.setStateCode(101);
		br.setStateMessage("获取信息成功");

		return br;
	}

	/**
	 * 删除意见反馈
	 * 
	 * @param feedbackId
	 * @return
	 * @throws Exception
	 */
	@Transactional(rollbackFor = Exception.class)
	public BaseResponse delfb(Integer feedbackId) throws Exception {

		Feedback feedback = feedbackDao.findById(feedbackId);
		if (feedback == null) {
			throw new ParameterException(301, "删除的信息不存在");
		}
		feedbackDao.updatestate(feedbackId);

		BaseResponse br = new BaseResponse();
		br.setStateCode(103);
		br.setStateMessage("删除信息成功");

		return br;
	}

	/**
	 * 意见阅读
	 * 
	 * @param feedbackId
	 * @return
	 * @throws Exception
	 */
	@Transactional(rollbackFor = Exception.class)
	public BaseResponse updafb(Integer feedbackId) throws Exception {

		Feedback feedback = feedbackDao.findById(feedbackId);
		if (feedback == null) {
			throw new ParameterException(301, "阅读的信息不存在");
		}
		feedbackDao.updatetype(feedbackId);

		BaseResponse br = new BaseResponse();
		br.setStateCode(102);
		br.setStateMessage("阅读成功");

		return br;
	}

	/**
	 * 获取公司列表
	 * 
	 * @return
	 * @throws Exception
	 */
	@Transactional(readOnly = true)
	public BaseResponse allcom() throws Exception {

		List<Company> list = companyDao.findByType(1, new Sort(Direction.ASC, "border"));

		List<Allcompany> data = new ArrayList<Allcompany>();
		for (Company company : list) {
			Allcompany a = new Allcompany();
			a.setAbroad(company.getBorder());
			a.setCompanyId(company.getId());
			a.setName(company.getName());
			a.setSearch(company.getIntroduction());
			data.add(a);
		}

		BaseResponse br = new BaseResponse();
		br.setData(data);
		br.setStateCode(101);
		br.setStateMessage("获取信息成功");

		return br;
	}

	/**
	 * 新增公司
	 * 
	 * @param addCompany
	 * @return
	 * @throws Exception
	 */
	@Transactional(rollbackFor = Exception.class)
	public BaseResponse addcm(AddCompany addCompany) throws Exception {
		Company a = new Company();
		a.setBorder(addCompany.getAbroad());
		a.setIntroduction(addCompany.getSearch());
		a.setName(addCompany.getName());
		a.setType(1);
		a.setTime(System.currentTimeMillis());

		companyDao.save(a);

		BaseResponse br = new BaseResponse();
		br.setStateCode(100);
		br.setStateMessage("添加成功");

		return br;
	}

	/**
	 * 修改公司
	 * 
	 * @param companyId
	 * @param addCompany
	 * @return
	 * @throws Exception
	 */
	@Transactional(rollbackFor = Exception.class)
	public BaseResponse updacm(Integer companyId, AddCompany addCompany) throws Exception {
		Company company = companyDao.findById(companyId);
		if (company == null) {
			throw new ParameterException(301, "修改的公司不存在");
		}

		companyDao.updatecom(companyId, addCompany.getAbroad(), addCompany.getName(), addCompany.getSearch());

		BaseResponse br = new BaseResponse();
		br.setStateCode(102);
		br.setStateMessage("修改成功");

		return br;
	}

	/**
	 * 删除公司
	 * 
	 * @param companyId
	 * @return
	 * @throws Exception
	 */
	@Transactional(rollbackFor = Exception.class)
	public BaseResponse delcm(Integer companyId) throws Exception {
		Company company = companyDao.findById(companyId);
		if (company == null) {
			throw new ParameterException(301, "删除的公司不存在");
		}
		companyDao.updatecp(companyId);
		BaseResponse br = new BaseResponse();
		br.setStateCode(103);
		br.setStateMessage("删除成功");

		return br;
	}

	/**
	 * 获取病种列表
	 * 
	 * @return
	 * @throws Exception
	 */
	@Transactional(readOnly = true)
	public BaseResponse alldis() throws Exception {

		AllDisss data = new AllDisss();

		List<Alldise> mildList = new ArrayList<Alldise>();// 轻疾
		List<Alldise> seriousList = new ArrayList<Alldise>();// 重疾
		List<Alldise> maleList = new ArrayList<Alldise>();// 男性特定
		List<Alldise> femaleList = new ArrayList<Alldise>();// 女性特定
		List<Alldise> childList = new ArrayList<Alldise>();// 儿童特定

		List<Disease> list1 = diseaseDao.findByTypeAndState(1, 1);
		for (Disease disease : list1) {
			Alldise a = new Alldise();
			a.setDiseaseId(disease.getId());
			a.setDiseaseName(disease.getName());

			mildList.add(a);
		}

		List<Disease> list2 = diseaseDao.findByTypeAndState(2, 1);
		for (Disease disease : list2) {
			Alldise a = new Alldise();
			a.setDiseaseId(disease.getId());
			a.setDiseaseName(disease.getName());

			seriousList.add(a);
		}

		List<Disease> list3 = diseaseDao.findByTypeAndState(31, 1);
		for (Disease disease : list3) {
			Alldise a = new Alldise();
			a.setDiseaseId(disease.getId());
			a.setDiseaseName(disease.getName());

			femaleList.add(a);
		}

		List<Disease> list4 = diseaseDao.findByTypeAndState(32, 1);
		for (Disease disease : list4) {
			Alldise a = new Alldise();
			a.setDiseaseId(disease.getId());
			a.setDiseaseName(disease.getName());

			maleList.add(a);
		}

		List<Disease> list5 = diseaseDao.findByTypeAndState(33, 1);
		for (Disease disease : list5) {
			Alldise a = new Alldise();
			a.setDiseaseId(disease.getId());
			a.setDiseaseName(disease.getName());

			childList.add(a);
		}

		data.setChildList(childList);
		data.setFemaleList(femaleList);
		data.setMaleList(maleList);
		data.setMildList(mildList);
		data.setSeriousList(seriousList);

		BaseResponse br = new BaseResponse();
		br.setData(data);
		br.setStateCode(101);
		br.setStateMessage("获取信息成功");

		return br;
	}

	/**
	 * 更新病种列表
	 * 
	 * @param updDis
	 * @return
	 * @throws Exception
	 */
	@Transactional(rollbackFor = Exception.class)
	public BaseResponse updatedis(UpdDis updDis) throws Exception {
		List<DelDislist> deleteList = updDis.getDeleteList(); // 删除的病种集合
		for (DelDislist delDislist : deleteList) {
			diseaseDao.upddis(delDislist.getDiseaseId());
		}
		List<ModisList> modifyList = updDis.getModifyList();// 修改的病种集合
		for (ModisList modisList : modifyList) {
			diseaseDao.upddisname(modisList.getDiseaseId(), modisList.getDiseaseName());
		}
		List<AddDisList> addList = updDis.getAddList();
		for (AddDisList addDisList : addList) {
			Disease a = new Disease();
			a.setName(addDisList.getDiseaseName());
			a.setState(1);
			a.setType(addDisList.getType());
			diseaseDao.save(a);
		}

		BaseResponse br = new BaseResponse();
		br.setStateCode(102);
		br.setStateMessage("修改成功");

		return br;
	}

}
